/**
 * 
 */
package com.android.hw.projectmenu;
import java.util.HashMap;

import com.android.hw.projectmenu.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.net.wifi.p2p.WifiP2pManager.ActionListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.Slog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.internal.telephony.Phone;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;

import com.android.internal.telephony.PhoneFactory;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.cdma.CDMAPhone;
/**
 * @author gaoshengpan
 *
 */
public class NetworkStatus extends Activity {
	private int numPhone = 0;
    private Phone mPhone = null;
    private static String mUnknown = null;
    private PhoneStateListener[] mMSimPhoneStateListener;
    private static int selectedItem;
    SignalStrength[] mMSimSignalStrength;
     String cdmaImsi,gsmImsi ;
     String[] mcc,mnc;
     int[] dbm,asu;
     private String TAG = "NetworkStatus";
     
	/**
	 * 
	 */
	public NetworkStatus() {
		// TODO Auto-generated constructor stub
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.network_status);
		final ActionBar actionBar = getActionBar();
		
        numPhone = TelephonyManager.getDefault().getPhoneCount();
        mcc = new String[numPhone];
        mnc = new String[numPhone];
        dbm = new int[4];
        asu = new int[numPhone];
        mMSimPhoneStateListener = new PhoneStateListener[numPhone];
        mMSimSignalStrength = new SignalStrength[numPhone];
        
        try {
            for (int i = 0; i < numPhone; i++) {
                Phone phone = PhoneFactory.getPhone(i);
                if (phone.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
                    mPhone = phone;
                    break;
                }
            }
		} catch (Exception e) {
			// TODO: handle exception
			Log.d(TAG, "e="+e);
		}
        Fragment card1 = new Card1Fragment();
        Fragment card2 = new Card2Fragment();
		FragmentTransaction ft1 = getFragmentManager().beginTransaction();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.addTab(actionBar.newTab().setText(getString(R.string.sim1)).setTabListener(new MyTabListener(card1)));
		actionBar.addTab(actionBar.newTab().setText(getString(R.string.sim2)).setTabListener(new MyTabListener(card2)));
		/*ft1.add(R.id.container, card1,"card1");
		ft1.commit();*/
			 for (int i = 0; i < numPhone ; i++) {
				 Phone phone = PhoneFactory.getPhone(i);
	                if (phone.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
	                    mPhone = phone;
	                }
	    		 int[] subId = SubscriptionManager.getSubId(i);
	    		 /*gaoshenpan 15-08-06 fix card1 subidChanged begin*/
	    		 Log.d(TAG,"TelephonyManager.getDefault().getSubscriberId(subId[0])!=null"+(TelephonyManager.getDefault().getSubscriberId(subId[0])!=null) );
	    		 Log.d(TAG,"subId[0]="+subId[0]+"i="+i);
	    		 
	    		/* if(TelephonyManager.getDefault().getSubscriberId(subId[0])!=null){ 
				if (subId[0] > 0) {*/
				
						mMSimPhoneStateListener[i] = getPhoneStateListener(subId[0], i);
						TelephonyManager.getDefault().listen(mMSimPhoneStateListener[i],PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
		
				
//				}
	    		 if( i==0 ){
	    			 cdmaImsi = TelephonyManager.getDefault().getSubscriberId(subId[0]);
	    			 if(cdmaImsi!=null&& cdmaImsi.length()>=6){
	    			 mcc[0] = cdmaImsi.substring(0, 4);
	    			 mnc[0] = cdmaImsi.substring(4, 6);
	    			 }
	    			 Log.d(TAG,"--------->00getDefaultSubId"+cdmaImsi+"\n"+mcc[0]+"\n"+mnc[0]);
	    		 }else{
	    			 gsmImsi = TelephonyManager.getDefault().getSubscriberId(subId[0]);
	    			 if(gsmImsi!=null && gsmImsi.length()>=6){
		    			 mcc[1] = gsmImsi.substring(0, 4);
		    			 mnc[1] = gsmImsi.substring(4, 6);
		    			 }
	    			 Log.d(TAG,"--------->11getDefaultSubId"+gsmImsi+"\n"+mcc[1]+"\n"+mnc[1]);
	    		 }
	    		 /*}*/
	    		 /*gaoshenpan 15-08-06 fix card1 subidChanged end*/
	    		 
	    		
	    		 
	    	 }
	}

    private class MyTabListener implements TabListener{
    	private Fragment fragment;
    	

    	/**
		 * 
		 */
		public MyTabListener(Fragment fragment) {
			// TODO Auto-generated constructor stub
			this.fragment = fragment;
		}
		/* (non-Javadoc)
		 * @see android.app.ActionBar.TabListener#onTabReselected(android.app.ActionBar.Tab, android.app.FragmentTransaction)
		 */
		@Override
		public void onTabReselected(Tab arg0, FragmentTransaction ft) {
			// TODO Auto-generated method stub
			
		}

		/* (non-Javadoc)
		 * @see android.app.ActionBar.TabListener#onTabSelected(android.app.ActionBar.Tab, android.app.FragmentTransaction)
		 */
		@Override
		public void onTabSelected(Tab arg0, FragmentTransaction ft) {
			// TODO Auto-generated method stub
			ft.add(R.id.container, fragment,"card1");
			
		}

		/* (non-Javadoc)
		 * @see android.app.ActionBar.TabListener#onTabUnselected(android.app.ActionBar.Tab, android.app.FragmentTransaction)
		 */
		@Override
		public void onTabUnselected(Tab arg0, FragmentTransaction ft) {
			// TODO Auto-generated method stub
			ft.remove(fragment);
		}
    	
    }
	
	
	public class Card1Fragment extends Fragment {
		/*
		 * (non-Javadoc)
		 * 
		 * @see android.app.Fragment#onCreateView(android.view.LayoutInflater,
		 * android.view.ViewGroup, android.os.Bundle)
		 */

		private String cdmaImsi;
		private String mcc, mnc;
		int dbm1, dbm2,dbm3,asu;
		private TextView mccView, mncView, signalView;
		private SignalStrength signalStrength;

		private Handler mHandler1 = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				/*gaoshenpan 15-08-06 fix card1 use LteDbm begin*/
				Log.d(TAG,"mMSimSignalStrength[0] != null"+(mMSimSignalStrength[0] != null) );
				if(mMSimSignalStrength[0] != null){
				dbm1 =  mMSimSignalStrength[0].getCdmaDbm();
				dbm2 =  mMSimSignalStrength[0].getEvdoDbm();
				dbm3 =  mMSimSignalStrength[0].getLteDbm();
				Log.d(TAG, "-------->"+dbm1+"        "+dbm2+"          "+dbm3);
				asu = mMSimSignalStrength[0].getAsuLevel();
				signalView.setText(getString(R.string.signalstreghth_info)
						+ "CdmaDbm="+dbm1+"dbm"+"    "+"EvdoDbm="+dbm2 + "dbm"+"\n"
						+"LteDbm="+dbm3 +"dbm"+"    "+"AsuLevel="+ asu + "asu");
				Log.d(TAG, "-------->"+"CdmaDbm-"+dbm1+"dbm"+"\n"+
						"EvdoDbm-"+dbm2 + "dbm"+"LteRsrp"+dbm3 +"dbm"+"\n"+"AsuLevel=" + asu + "asu");
				}
				/*gaoshenpan 15-08-06 fix card1 use LteDbm end*/
				mHandler1.sendEmptyMessageDelayed(0, 1000);
			}
		};

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			Log.d(TAG,
					"--------------->"
							+ "onCreateView(LayoutInflater inflater, ViewGroup container,");
			// TODO Auto-generated method stub
			View view = inflater.inflate(R.layout.card1, container, false);
			int[] subId = SubscriptionManager.getSubId(0);
			/*gaoshenpan 15-08-06 fix card1 subidChanged begin*/
			Log.d(TAG, "TelephonyManager.getDefault().getSubscriberId(subId[0])="+TelephonyManager.getDefault().getSubscriberId(subId[0]));
			if (TelephonyManager.getDefault().getSubscriberId(subId[0]) != null) {
//				if (subId[0] > 0) {
					mMSimPhoneStateListener[0] = getPhoneStateListener(
							subId[0], 0);
					TelephonyManager.getDefault().listen(
							mMSimPhoneStateListener[0],
							PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
					mHandler1.sendEmptyMessage(0);
					cdmaImsi = TelephonyManager.getDefault().getSubscriberId(
							subId[0]);
					if (cdmaImsi != null && cdmaImsi.length()>=6) {
						mcc = cdmaImsi.substring(0, 4);
						mnc = cdmaImsi.substring(4, 6);
					}
//				}
					/*gaoshenpan 15-08-06 fix card1 subidChanged end*/
			}
			mccView = (TextView) view.findViewById(R.id.textView1);
			mncView = (TextView) view.findViewById(R.id.textView2);
			signalView = (TextView) view.findViewById(R.id.textView3);
			mccView.setText(getString(R.string.mcc_info) + mcc);
			mncView.setText(getString(R.string.mnc_info) + mnc);

			if (view != null) {
				Log.d(TAG, "--------------->" + "view not null");
			}
			return view;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.app.Fragment#onResume()
		 */
		@Override
		public void onResume() {
			// TODO Auto-generated method stub
			super.onResume();
			mHandler1.sendEmptyMessage(0);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.app.Fragment#onPause()
		 */
		@Override
		public void onPause() {
			// TODO Auto-generated method stub
			super.onPause();
			mHandler1.removeMessages(0);
		}
	}
	
	public class Card2Fragment extends Fragment {

		private String gsmImsi;
		private String mcc, mnc;
		int dbm, asu,lac,cellID;
		private TextView mccView, mncView, signalView, lacView, cellIDView;
		private SignalStrength signalStrength;
		private GsmCellLocation cellLocation;

		private Handler mHandler2 = new Handler() {
			int lactemp,cellIDtemp;

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if(mMSimSignalStrength[1] != null){
				dbm = mMSimSignalStrength[1].getGsmDbm();
				asu = mMSimSignalStrength[1].getAsuLevel();
				signalView.setText(getString(R.string.signalstreghth_info)
						+ dbm + "dbm" + asu + "asu");
				}
				if (cellLocation != null) {
                  lactemp = cellLocation.getLac();
                  cellIDtemp = cellLocation.getCid();
                  if(lactemp > 0 && lactemp !=65535 ){
                	  lac = lactemp;
                  }
                  if(cellIDtemp > 0 && cellIDtemp !=65535 ){
                	  cellID = cellIDtemp;
                  }
                  lacView.setText(getString(R.string.lac_info)+lac);
                  cellIDView.setText(getString(R.string.cellid_info)+cellID);
				}
				mHandler2.sendEmptyMessageDelayed(0, 1000);
			}
		};

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.app.Fragment#onCreateView(android.view.LayoutInflater,
		 * android.view.ViewGroup, android.os.Bundle)
		 */
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			Log.d(TAG,
					"--------------->"
							+ "onCreateView(LayoutInflater inflater, ViewGroup container,2");
			// TODO Auto-generated method stub
			View view = inflater.inflate(R.layout.card2, container, false);
			int[] subId = SubscriptionManager.getSubId(1);
			if(mPhone != null){
				 cellLocation = (GsmCellLocation) (mPhone
	                    .getCellLocation());
			}
			/*gaoshenpan 15-08-06 fix no need judge gsmImsi null for card2 begin*/
			/*if (TelephonyManager.getDefault().getSubscriberId(subId[0]) != null) {*/
				if (subId[0] > 0) {
					mMSimPhoneStateListener[1] = getPhoneStateListener(
							subId[0], 1);
					TelephonyManager.getDefault().listen(
							mMSimPhoneStateListener[1],
							PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
					mHandler2.sendEmptyMessage(0);
					gsmImsi = TelephonyManager.getDefault().getSubscriberId(
							subId[0]);
					if (gsmImsi != null && gsmImsi.length()>=6) {
						mcc = gsmImsi.substring(0, 4);
						mnc = gsmImsi.substring(4, 6);
					}
				}
//			}
			/*gaoshenpan 15-08-06 fix no need judge gsmImsi null for card2 end*/
			mccView = (TextView) view.findViewById(R.id.textView1);
			mncView = (TextView) view.findViewById(R.id.textView2);
			signalView = (TextView) view.findViewById(R.id.textView3);
			lacView = (TextView) view.findViewById(R.id.textView4);
			cellIDView = (TextView) view.findViewById(R.id.textView5);
			mccView.setText(getString(R.string.mcc_info) + mcc);
			mncView.setText(getString(R.string.mnc_info) + mnc);
			if (view != null) {
				Log.d(TAG, "--------------->" + "view not null 2");
			}
			return view;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.app.Fragment#onPause()
		 */
		@Override
		public void onPause() {
			// TODO Auto-generated method stub
			super.onPause();
			mHandler2.removeMessages(0);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.app.Fragment#onResume()
		 */
		@Override
		public void onResume() {
			// TODO Auto-generated method stub
			super.onResume();
			mHandler2.sendEmptyMessage(0);
		}
	}
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	private PhoneStateListener getPhoneStateListener(int subId, int slotId) {
		
		PhoneStateListener mMSimPhoneStateListener = new PhoneStateListener(subId) {
			 /* (non-Javadoc)
			 * @see android.telephony.PhoneStateListener#onSignalStrengthsChanged(android.telephony.SignalStrength)
			 */
			@Override
			public void onSignalStrengthsChanged(SignalStrength signalStrength) {
				// TODO Auto-generated method stub
				super.onSignalStrengthsChanged(signalStrength);
				 mMSimSignalStrength[SubscriptionManager.getPhoneId(mSubId)] = signalStrength;
				Log.d(TAG, "onSignalStrengthsChanged----------->mSubId="+mSubId+"\n"+"dbm="+signalStrength.getDbm()+"\n"+"asu="+signalStrength.getAsuLevel());
			}
		};

		return mMSimPhoneStateListener;
	}
	
}
